package com.cndw.kungfu.domain;

import java.util.List;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.ext.JsonProcesser;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.battle.FighterMonster;
import com.cndw.kungfu.vo.MonsterVO;

/**
 * 系统怪物对象
 * 
 * @author fantadust
 * 
 */
public class Monster implements Cloneable {

	private static long signNum = 0;
	private int id;// 编号
	private String name;// 名称
	private String icon;// 图片
	/**
	 * 类型 0 近战,1远程
	 */
	private int type;// 类型
	private int level;// 等级
	private int hp;// 血
	private int mp;// 蓝
	private int attack;// 攻击
	private int defense;
	private int hit;// 命中
	private int crit;// 闪避
	private int tough;
	private int dodge;
	private int attackSpeed;
	private int addition1;
	private int addition2;
	private int addition3;
	private int assetR;// 掉钱几率
	private int asset;// 掉钱数量
	private int exp;// 经验值
	/**
	 * 0普通 1精英 2 boss
	 */
	private int boss;// 是否boss
	private String itemFallData;// 掉落物品数据
	private int itemFallMax;// 最大掉落数
	private int talkR;// 说话几率
	private String talkData;// 说话内容
	/**
	 * 怪物移动速度
	 */
	private int moveSpeed;

	private boolean cloned = false;
	private int maxHp;
	private int maxMp;
	private Long uniqueId = 0L;

	private int[] inScend;

	/**
	 * 是否主动攻击
	 */
	private boolean initiative;
	/**
	 * ai
	 */
	private int ai;

	private List<GoodsFall> itemFallDataList;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getHp() {
		return hp;
	}

	public void setHp(int hp) {
		this.hp = hp;
	}

	public int getMp() {
		return mp;
	}

	public void setMp(int mp) {
		this.mp = mp;
	}

	public int getAttack() {
		return attack;
	}

	public void setAttack(int attack) {
		this.attack = attack;
	}

	public int getDefense() {
		return defense;
	}

	public void setDefense(int defense) {
		this.defense = defense;
	}

	public int getHit() {
		return hit;
	}

	public void setHit(int hit) {
		this.hit = hit;
	}

	public int getCrit() {
		return crit;
	}

	public void setCrit(int crit) {
		this.crit = crit;
	}

	public int getTough() {
		return tough;
	}

	public void setTough(int tough) {
		this.tough = tough;
	}

	public int getDodge() {
		return dodge;
	}

	public void setDodge(int dodge) {
		this.dodge = dodge;
	}

	public int getAttackSpeed() {
		return attackSpeed;
	}

	public void setAttackSpeed(int attackSpeed) {
		this.attackSpeed = attackSpeed;
	}

	public int getAddition1() {
		return addition1;
	}

	public void setAddition1(int addition1) {
		this.addition1 = addition1;
	}

	public int getAddition2() {
		return addition2;
	}

	public void setAddition2(int addition2) {
		this.addition2 = addition2;
	}

	public int getAddition3() {
		return addition3;
	}

	public void setAddition3(int addition3) {
		this.addition3 = addition3;
	}

	public int getAssetR() {
		return assetR;
	}

	public void setAssetR(int assetR) {
		this.assetR = assetR;
	}

	public int getAsset() {
		return asset;
	}

	public void setAsset(int asset) {
		this.asset = asset;
	}

	public int getExp() {
		return exp;
	}

	public void setExp(int exp) {
		this.exp = exp;
	}

	public int getBoss() {
		return boss;
	}

	public void setBoss(int boss) {
		this.boss = boss;
	}

	public int getItemFallMax() {
		return itemFallMax;
	}

	public void setItemFallMax(int itemFallMax) {
		this.itemFallMax = itemFallMax;
	}

	public String getItemFallData() {
		return itemFallData;
	}

	public void setItemFallData(String itemFallData) {
		this.itemFallData = itemFallData;
		if (itemFallData != null && itemFallData.length() > 0) {
			try {
				itemFallDataList = JsonProcesser.objectJsonArrayDecode(itemFallData, GoodsFall.class);
			} catch (Exception e) {
				LogUtil.getMain().error("Monster config err | id: " + id + "|itemFallData|" + itemFallData);
				// e.printStackTrace();
			}

		}

	}

	public List<GoodsFall> getItemFallDataList() {
		return itemFallDataList;
	}

	public void setItemFallDataList(List<GoodsFall> itemFallDataList) {
		this.itemFallDataList = itemFallDataList;
	}

	public int getTalkR() {
		return talkR;
	}

	public void setTalkR(int talkR) {
		this.talkR = talkR;
	}

	public String getTalkData() {
		return talkData;
	}

	public void setTalkData(String talkData) {
		this.talkData = talkData;
	}

	public int[] getInScend() {
		return inScend;
	}

	public void setInScend(int[] inScend) {
		this.inScend = inScend;
	}

	public Long getUniqueId() {
		return uniqueId;
	}

	public void setUniqueId(Long uniqueId) {
		this.uniqueId = uniqueId;
	}

	public int getMaxHp() {
		return maxHp;
	}

	public void setMaxHp(int maxHp) {
		this.maxHp = maxHp;
	}

	public int getMaxMp() {
		return maxMp;
	}

	public void setMaxMp(int maxMp) {
		this.maxMp = maxMp;
	}

	public int getMoveSpeed() {
		return moveSpeed;
	}

	public void setMoveSpeed(int moveSpeed) {
		this.moveSpeed = moveSpeed;
	}

	public boolean isInitiative() {
		return initiative;
	}

	public void setInitiative(boolean initiative) {
		this.initiative = initiative;
	}

	public int getAi() {
		return ai;
	}

	public void setAi(int ai) {
		this.ai = ai;
	}

	public MonsterVO toMonsterVo() {
		return new MonsterVO(this);
	}

	/**
	 * 设置血量
	 * 
	 * @param alter
	 */
	public synchronized void alterHp(int alter) {
		int newHp = NumberProcesser.between(alter + getHp(), 0, getMaxHp());
		setHp(newHp);
	}

	/**
	 * 设置魔法量
	 * 
	 * @param alter
	 */
	public synchronized void alterMp(int alter) {
		int newMp = NumberProcesser.between(alter + getMp(), 0, getMaxMp());
		setMp(newMp);
	}

	private void initUniqueIdAndHpMp() {
		signNum--;
		if (signNum <= Integer.MIN_VALUE || signNum >= 0) {
			signNum = -1;
		}
		setUniqueId(signNum);
		initRandomProperty();
		maxHp = hp;// 初始最大血量
		maxMp = mp;
	}

	/**
	 * 把3项随机属性进行分配
	 */
	private void initRandomProperty() {
		if (cloned) {// 只处理克隆后的怪物对象
			try {
				if (addition1 > 0) {
					int[] xxx = NumberProcesser.getRandomArray(addition1, 3);
					attack += xxx[0];
					defense += xxx[1];
					attackSpeed += xxx[2];

				}
				if (addition2 > 0) {
					int[] xxx = NumberProcesser.getRandomArray(addition2, 2);
					hp += xxx[0];
					maxHp += xxx[0];
					mp += xxx[1];
					maxMp += xxx[1];

				}
				if (addition3 > 0) {
					int[] xxx = NumberProcesser.getRandomArray(addition3, 4);
					hit += xxx[0];
					crit += xxx[1];
					dodge += xxx[2];
					tough += xxx[3];
				}

				if (boss == 3) {
					int coff = TimeProcesser.dayInterval(ConstantsConfig.getSysCgOpenTime());
					coff = NumberProcesser.between(coff, 0, 18);
					hp = hp / 10 * (2 + coff);
					attack = attack / 20 * (2 + coff);
					maxHp = hp;
				} else if (boss == 4 || boss == 5 ) {
					int coff = TimeProcesser.dayInterval(ConstantsConfig.getSysCgOpenTime());
					coff = NumberProcesser.between(coff, 0, 8);
					hp = hp / 10 * (2 + coff);
					attack = attack / 20 * (2 + coff);
					maxHp = hp;
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public Monster clone() {
		Monster o = null;
		try {
			o = (Monster) super.clone();
			o.cloned = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return o;
	}

	public FighterMonster fighter() {
		Monster fighterMonster = clone();
		fighterMonster.initUniqueIdAndHpMp();
		return new FighterMonster(fighterMonster);
	}

}
